package com.sy.cloud.wechat.service;

import com.sy.cloud.wechat.ConstantsPlaceholder;
import com.sy.cloud.wechat.WeChatConfig;
import com.sy.cloud.wechat.WeChatException;
import com.sy.cloud.wechat.entity.UserAccessToken;
import com.sy.cloud.wechat.entity.WeChatUserInfo;
import com.sy.cloud.wechat.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * Created by zhuwenquan on 2017-10-29.
 */
@Service
@Slf4j
public class AuthorizeService {

        public String getAuthorizeBaseUrl(String appId, String redirectUri, String state) {
            try {
                String apiUrl = WeChatConfig.getUrl("authorize.get.code")
                        .replace(ConstantsPlaceholder.APPID, appId)
                        .replace(ConstantsPlaceholder.REDIRECT_URI, URLEncoder.encode(redirectUri, "UTF-8"))
                        .replace(ConstantsPlaceholder.SCOPE, "snsapi_base")
                        .replace(ConstantsPlaceholder.STATE, state);
                log.info("网址");
                log.info( apiUrl );
                return apiUrl;
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }

        public String getAuthorizeUserInfoUrl(String appId, String redirectUri, String state) {
            try {
                String apiUrl = WeChatConfig.getUrl("authorize.get.code")
                        .replace(ConstantsPlaceholder.APPID, appId)
                        .replace(ConstantsPlaceholder.REDIRECT_URI, URLEncoder.encode(redirectUri, "UTF-8"))
                        .replace(ConstantsPlaceholder.SCOPE, "snsapi_userinfo")
                        .replace(ConstantsPlaceholder.STATE, state);
                return apiUrl;
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }


        public UserAccessToken getUserAccessTokenByCode(String appId, String secret, String code) {
            String apiUrl = WeChatConfig.getUrl("useraccesstoken.get")
                    .replace(ConstantsPlaceholder.APPID, appId)
                    .replace(ConstantsPlaceholder.SECRET, secret)
                    .replace(ConstantsPlaceholder.CODE, code);
            UserAccessToken userAccessToken = HttpUtils.get(apiUrl, UserAccessToken.class);

            if (userAccessToken.isSuccess()) {
                return userAccessToken;
            } else {
                throw new WeChatException("get useraccesstoken error " + userAccessToken.toErrorString());
            }
        }

        public WeChatUserInfo getWeChatUnSubscribeUserInfo(String userAccessToken, String openId) {
            String apiUrl = WeChatConfig.getUrl("useraccesstoken.get.userinfo")
                    .replace(ConstantsPlaceholder.ACCESS_TOKEN, userAccessToken)
                    .replace(ConstantsPlaceholder.OPENID, openId);
            WeChatUserInfo weChatUserInfo = HttpUtils.get(apiUrl, WeChatUserInfo.class);

            if (weChatUserInfo.isSuccess()) {
                return weChatUserInfo;
            } else {
                throw new WeChatException("get weChat unSubscribe userInfo error " + weChatUserInfo.toErrorString());
            }
        }

        public WeChatUserInfo getWeChatSubscribeUserInfo(String accessToken,String openId){
            String apiUrl = WeChatConfig.getUrl("get.user.info")
                    .replace(ConstantsPlaceholder.ACCESS_TOKEN, accessToken)
                    .replace(ConstantsPlaceholder.OPENID, openId);
            log.info("WeChatUserInfo:"+apiUrl);
            WeChatUserInfo weChatUserInfo = HttpUtils.get(apiUrl, WeChatUserInfo.class);

            if (weChatUserInfo.isSuccess()) {
                return weChatUserInfo;
            } else {
                throw new WeChatException("get weChat subscribe userInfo error" + weChatUserInfo.toErrorString());
            }
        }

        public UserAccessToken checkUserAccessToken(String userAccessToken, String openId) {
            String apiUrl = WeChatConfig.getUrl("useraccesstoken.check")
                    .replace(ConstantsPlaceholder.ACCESS_TOKEN, userAccessToken)
                    .replace(ConstantsPlaceholder.OPENID, openId);
            UserAccessToken accessToken = HttpUtils.get(apiUrl, UserAccessToken.class);

            if (accessToken.isSuccess()) {
                return accessToken;
            } else {
                throw new WeChatException("check useraccesstoken error " + accessToken.toErrorString());
            }
        }

        public UserAccessToken refreshUserAccessToken(String appId, String refreshToken) {
            String apiUrl = WeChatConfig.getUrl("useraccesstoken.refresh")
                    .replace(ConstantsPlaceholder.APPID, appId)
                    .replace(ConstantsPlaceholder.REFRESH_TOKEN, refreshToken);
            UserAccessToken userAccessToken = HttpUtils.get(apiUrl, UserAccessToken.class);

            if (userAccessToken.isSuccess()) {
                return userAccessToken;
            } else {
                throw new WeChatException("get refresh useraccesstoken error " + userAccessToken.toErrorString());
            }
        }


    }
